using System;
using System.Collections.Generic;
using System.Text;
using Csla.Data;
using Csla;
using Active.Core.DB;
using Active.Core.Command;

namespace Active.Core
{
    [Serializable()]
    public class MediaOrders :
      BusinessListBase<MediaOrders, MediaOrder>
    {
        #region Authorization Rules
        /// <summary>
        /// Determines whether this instance [can get object].
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if this instance [can get object]; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanGetObject()
        {
            return Utils.IsMediaEmployee || Utils.IsMediaManager
                || Utils.IsGeneralManager || Utils.IsFinanceEmployee;
        }

        #endregion

        #region Business Methods
        protected override object AddNewCore()
        {
            if (!MediaOrder.CanAddObject())
                throw new System.Security.SecurityException(
                  "User not authorized to add an mediaOrder");
            MediaOrder order = MediaOrder.NewMediaOrder();
            Add(order);
            return order;
        }

        /// <summary>
        /// Gets the media order by id.
        /// </summary>
        /// <param name="mediaOrderId">The media order id.</param>
        /// <returns></returns>
        public MediaOrder GetMediaOrderById(int mediaOrderId)
        {
            foreach (MediaOrder item in this)
            {
                if (item.Id == mediaOrderId)
                    return item;
            }
            return null;
        }



        /// <summary>
        /// Removes the specified media order id.
        /// </summary>
        /// <param name="mediaOrderId">The media order id.</param>
        public void Remove(int mediaOrderId)
        {
            if (!MediaOrder.CanDeleteObject())
                throw new System.Security.SecurityException(
                  "User not authorized to remove an mediaOrder");

            foreach (MediaOrder item in this)
            {
                if (item.Id == mediaOrderId)
                {
                    Remove(item);
                    break;
                }
            }
        }

        #endregion

        #region Factory Methods

        internal static MediaOrders NewMediaOrders()
        {
            return new MediaOrders();
        }

        internal static MediaOrders GetMediaOrders(SafeDataReader dr)
        {
            return new MediaOrders(dr);
        }

        internal static MediaOrders GetMediaOrders(int customerOrderId)
        {
            return DataPortal.Fetch<MediaOrders>(new Criteria(customerOrderId));
        }

        private MediaOrders()
        {
            MarkAsChild();
        }

        private MediaOrders(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        [Serializable()]
        private class Criteria
        {
            private int _id;
            public int Id
            {
                get { return _id; }
            }

            public Criteria(int id)
            {
                _id = id;
            }
        }


        private void Fetch(SafeDataReader dr)
        {
            RaiseListChangedEvents = false;
            while (dr.Read())
            {
                this.Add(MediaOrder.GetMediaOrder(dr));
            }
            RaiseListChangedEvents = true;
        }

        internal void Update(Order parent)
        {
            RaiseListChangedEvents = false;
            foreach (MediaOrder item in DeletedList)
                item.DeleteSelf();
            DeletedList.Clear();

            foreach (MediaOrder item in this)
                if (item.IsNew)
                    item.Insert(parent);
                else if(item.IsDirty)
                    item.Update(parent);
            RaiseListChangedEvents = true;
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            NBearLite.SelectSqlSection select =
                DataService.DBService.Select(Tables.MediaOrders)
                .Where(Tables.MediaOrders.CustomerOrderId == criteria.Id);
            using (SafeDataReader dr = new SafeDataReader(select.ToDataReader()))
            {
                Fetch(dr);
            }
        }

        #endregion

        #region GetUsedPropertyList

        /// <summary>
        /// Gets the used property list.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="recordsCount">The records count.</param>
        /// <returns></returns>
        public static string[] GetUsedPropertyList(string propertyName, string prefix, int recordsCount)
        {
            GetPropertyList<string> cmd =
                new GetPropertyList<string>("MediaOrders", propertyName, prefix, recordsCount);
            cmd = DataPortal.Execute<GetPropertyList<string>>(cmd);
            return cmd.Result;
        }

        #endregion
    }
}

